package cn.ztuo.bitrade.dao;

import cn.ztuo.bitrade.constant.BooleanEnum;
import cn.ztuo.bitrade.entity.ContractOrderPositionType;
import cn.ztuo.bitrade.entity.ContractPositionForce;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author HJ
 */
public interface ContractPositionForceMapper extends JpaRepository<ContractPositionForce, String>, JpaSpecificationExecutor<ContractPositionForce>, QueryDslPredicateExecutor<ContractPositionForce> {

     List<ContractPositionForce> findByMemberIdAndSymbol(Long memberId, String symbol);
     List<ContractPositionForce> findAllByStatusAndIsLock(Integer status, BooleanEnum isLock);
     ContractPositionForce getByPositionTypeAndSymbolAndMemberId(ContractOrderPositionType positionType, String symbol, Long memberId);
     /**
      * 开仓 余额 --> 持仓
      *
      * @return
      */
     @Modifying
     @Query("update ContractPositionForce position set" +
             "        position.positionTotalValue = position.positionTotalValue + :amount," +
             "        position.contractTotalNum = position.contractTotalNum + :tradeNwop," +
             "        position.contractOccupyNum = position.contractOccupyNum + :tradeNwop," +
             "        position.holdingBond = position.holdingBond + :bond," +
             "        position.positionAvgPrice = :avgPrice " +
             "        where position.id = :id "
             )
     int updatePositionByDesignate(@Param("id") Long id,
                                   @Param("amount") BigDecimal amount,
                                   @Param("tradeNwop") Integer tradeNwop,
                                   @Param("avgPrice") BigDecimal avgPrice,
                                   @Param("bond") BigDecimal bond);
     /**
      * 开仓 余额 --> 持仓
      *
      * @return
      */
     @Modifying
     @Query("update ContractPositionForce position set" +
             "        position.positionTotalValue = position.positionTotalValue + :amount," +
             "        position.contractTotalNum = position.contractTotalNum + :tradeNwop," +
             "        position.contractOccupyNum = position.contractOccupyNum + :tradeNwop," +
             "        position.holdingBond = position.holdingBond + :bond," +
             "        position.positionAvgPrice = :avgPrice " +
             "        where position.id = :id "
             )
     int updatePositionByCancel(@Param("id") Long id,
                                @Param("amount") BigDecimal amount,
                                @Param("tradeNwop") Integer tradeNwop,
                                @Param("avgPrice") BigDecimal avgPrice,
                                @Param("bond") BigDecimal bond);
     /**
      * 平仓 持仓 --> 余额
      *
      * @return
      */
     @Modifying
     @Query("update ContractPositionForce position set" +
             "        position.positionTotalValue = position.positionTotalValue - :amount," +
             "        position.contractTotalNum = position.contractTotalNum - :tradeNwop," +
             "        position.holdingBond = position.holdingBond - :bond " +
             "        where position.id = :id "
             )
     int updatePositionByThrowout(@Param("id") Long id,
                                  @Param("amount") BigDecimal amount,
                                  @Param("tradeNwop") Integer tradeNwop,
                                  @Param("bond") BigDecimal bond);


}
